package org.lfh.wintersun.web.secondKill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.lfh.wintersun.common.base.PageUtils;
import org.lfh.wintersun.web.secondKill.dao.SecondKillVoucherExtDao;
import org.lfh.wintersun.web.secondKill.entity.SecondKillVoucherExtEntity;
import org.lfh.wintersun.web.secondKill.service.SecondKillVoucherExtService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 秒杀优惠券表，与优惠券是一对一关系(SecondKillVoucherExt)表服务实现类
 *
 * @author lfh
 * @since 2025-04-12 23:04:30
 */
@Service("secondKillVoucherExtService")
public class SecondKillVoucherExtServiceImpl extends ServiceImpl<SecondKillVoucherExtDao, SecondKillVoucherExtEntity>
        implements SecondKillVoucherExtService {

    /**
     * 通过ID查询单条数据
     *
     * @param voucherId 主键
     * @return 实例对象
     */
    @Override
    public SecondKillVoucherExtEntity queryById(String voucherId) {
        return this.getById(voucherId);
    }

    /**
     * 分页查询
     *
     * @param pageNum 页码
     * @param size    每页数量
     * @return 分页结果
     */
    @Override
    public PageUtils<SecondKillVoucherExtEntity> queryByPage(Integer pageNum, Integer size) {
        IPage<SecondKillVoucherExtEntity> iPage =
                this.baseMapper.queryAllByLimit(new Page<>(pageNum, size));
        return new PageUtils<>(iPage);
    }

    /**
     * 新增数据
     *
     * @param secondKillVoucherExtEntity 实例对象
     * @return 实例对象
     */
    @Override
    public SecondKillVoucherExtEntity insert(SecondKillVoucherExtEntity secondKillVoucherExtEntity) {
        this.save(secondKillVoucherExtEntity);
        return secondKillVoucherExtEntity;
    }

    /**
     * 修改数据
     *
     * @param secondKillVoucherExtEntity 实例对象
     * @return 实例对象
     */
    @Override
    public SecondKillVoucherExtEntity update(SecondKillVoucherExtEntity secondKillVoucherExtEntity) {
        this.updateById(secondKillVoucherExtEntity);
        return secondKillVoucherExtEntity;
    }

    /**
     * 通过主键删除数据
     *
     * @param voucherId 主键
     */
    @Override
    public void deleteById(Long voucherId) {
        LambdaUpdateWrapper<SecondKillVoucherExtEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(SecondKillVoucherExtEntity::getIsDeleted, 1);
        updateWrapper.eq(SecondKillVoucherExtEntity::getVoucherId, voucherId);
        this.update(updateWrapper);
    }

    /**
     * 通过主键集合删除数据
     *
     * @param voucherIds 主键集合
     */
    @Override
    public void deleteIds(List<Long> voucherIds) {
        super.removeByIds(voucherIds);
    }

    @Override
    public SecondKillVoucherExtEntity getOneByVoucherId(Long voucherId) {
        return this.getOne(new LambdaQueryWrapper<SecondKillVoucherExtEntity>()
                .eq(SecondKillVoucherExtEntity::getVoucherId, voucherId));
    }

    @Override
    public List<SecondKillVoucherExtEntity> listByVoucherIds(List<Long> voucherIds) {
        return this.list(new LambdaQueryWrapper<SecondKillVoucherExtEntity>()
                .in(SecondKillVoucherExtEntity::getVoucherId, voucherIds));
    }
}
